package mango.web.client;

import java.util.HashMap;

import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class View extends Bean {
	public AbsolutePanel panel = new AbsolutePanel();

	public String getCheckBoxString(HashMap<String, CheckBox> map) {
		String result = "";
		for (String key : map.keySet()) {
			if (map.get(key).getValue() == true) {
				result += map.get(key).getText().toUpperCase() + "&&";
			}
		}
		return result;
	}

	public String getRadioButtonString(HashMap<String, RadioButton> map) {
		String result = "";
		for (String key : map.keySet()) {
			if (map.get(key).getValue() == true) {
				result = map.get(key).getText().toUpperCase();
				break;
			}
		}
		return result;
	}

	public Integer getListboxInteger(ListBox listBox) {
		Integer result = null;
		String value = listBox.getValue(listBox.getSelectedIndex());
		if ("".equals(value)) return null;
		result = (Integer) Integer.parseInt(value);
		return result;
	}

	public String getListboxValue(ListBox listBox) {
		return listBox.getValue(listBox.getSelectedIndex());
	}

	public int setListboxSelected(ListBox listBox, Object value) {
		for (int i = 0; i < listBox.getItemCount(); i++) {
			if (listBox.getValue(i).equals(value + "")) {
				listBox.setSelectedIndex(i);
				return i;
			}
		}
		return 0;
	}

	public Label newLabel(String text, int width) {
		Label label = new Label(text);
		label.getElement().getStyle().setProperty("textAlign", "right");
		label.setSize(width + "px", "13px");
		return label;
	}

	public TextBox newTextBox(String text, int width) {
		TextBox textBox = new TextBox();
		textBox.setText(text);
		textBox.setSize(width + "px", "13px");
		return textBox;
	}

	public String getFirstLine(String str) {
		String result = str;
		int index = str.indexOf("\n");
		if (index != -1) {
			result = str.substring(0, str.indexOf("\n"));
		}
		return result;
	}

	public String getString(Object str) {
		if (str == null) return "";
		return str + "";
	}

	public void onLight(Widget widget) {
		widget.getElement().getStyle().setBackgroundColor("blue");
		widget.getElement().getStyle().setColor("white");
		widget.getElement().getStyle().setFontWeight(FontWeight.BOLD);
	}

	public void offLight(Widget widget) {
		widget.getElement().getStyle().setBackgroundColor("white");
		widget.getElement().getStyle().setColor("black");
		widget.getElement().getStyle().clearFontWeight();
	}

	public void tableRowRemove(FlexTable table) {
		for (int i = table.getRowCount() - 1; i >= 0; i--) {
			table.removeRow(i);
		}
	}

	public void tableRowRemove1(FlexTable table) {
		for (int i = table.getRowCount() - 1; i >= 1; i--) {
			table.removeRow(i);
		}
	}

	public int getWidth(Widget widget) {
		String str = widget.getElement().getStyle().getWidth();
		if (str.endsWith("px")) str = str.substring(0, str.length() - 2);
		return Integer.parseInt(str);
	}
}
